<?php

namespace Ibw\JobeetBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Affiliate
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Ibw\JobeetBundle\Entity\AffiliateRepository")
 */
class Affiliate {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="url", type="string", length=255)
     */
    private $url;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="token", type="string", length=255)
     */
    private $token;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $createdAt;

    /**
     * @ORM\ManyToOne(targetEntity="Category")
     * @ORM\JoinTable(name="category_affiliate",
     *      joinColumns={@ORM\JoinColumn(name="affiliate_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}
     *      )
     * */
    private $categories;

    function __construct() {
        $this->createdAt = $this->updatedAt = new \DateTime();
        $this->isActivated = 1;
        $this->token = 'asd' . rand(1, 1000);
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set url
     *
     * @param string $url
     * @return Affiliate
     */
    public function setUrl($url) {
        $this->url = $url;

        return $this;
    }

    /**
     * Get url
     *
     * @return string 
     */
    public function getUrl() {
        return $this->url;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return Affiliate
     */
    public function setEmail($email) {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail() {
        return $this->email;
    }

    /**
     * Set token
     *
     * @ORM\PrePersist
     * @param string $token
     * @return Affiliate
     */
    public function setToken($token) {
        if (!$this->getToken()) {
            $token = sha1($this->getEmail() . rand(11111, 99999));
            $this->token = $token;
        }

        return $this;
    }

    /**
     * Get token
     *
     * @return string 
     */
    public function getToken() {
        return $this->token;
    }

    /**
     * Set isActive
     *
     * @param boolean $isActive
     * @return Affiliate
     */
    public function setIsActive($isActive) {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive() {
        return $this->isActive;
    }

    /**
     * Set createdAt
     * @ORM\PrePersist
     * @param \DateTime $createdAt
     * @return Affiliate
     */
    public function setCreatedAt($createdAt) {
        if (!$this->getCreatedAt()) {
            $this->created_at = new \DateTime;
        }
    }

    /**
     * Get createdAt
     *
     * @return \DateTime 
     */
    public function getCreatedAt() {
        return $this->createdAt;
    }

    /**
     * Set category
     *
     * @param \Ibw\JobeetBundle\Entity\Category $category
     * @return Affiliate
     */
    public function setCategory(\Ibw\JobeetBundle\Entity\Category $category = null) {
        $this->category = $category;

        return $this;
    }

    /**
     * Get category
     *
     * @return \Ibw\JobeetBundle\Entity\Category 
     */
    public function getCategory() {
        return $this->category;
    }

    /**
     * Set categories
     *
     * @param \Ibw\JobeetBundle\Entity\Category $categories
     * @return Affiliate
     */
    public function setCategories(\Ibw\JobeetBundle\Entity\Category $categories = null) {
        $this->categories = $categories;

        return $this;
    }

    /**
     * Get categories
     *
     * @return \Ibw\JobeetBundle\Entity\Category 
     */
    public function getCategories() {
        return $this->categories;
    }

    public function activate() {
        if (!$this->getIsActive()) {
            $this->setIsActive(true);
        }

        return $this->is_active;
    }

    public function deactivate() {
        if ($this->getIsActive()) {
            $this->setIsActive(false);
        }

        return $this->is_active;
    }

}
